/// <reference path="../Scripts/jquery-1.7.1.min.js" />
/// <reference path="../Scripts/knockout-2.1.0.js" />

function LogCategoryViewModel() 
	{
    var self = this;
	// put a property loop here
	self.LogCategoryID = ko.observable();
			self.LogCategoryName = ko.observable();
			self.TS = ko.observable();
			self.CreatedBy = ko.observable();
			self.CreatedDt = ko.observable();
			self.ModifiedBy = ko.observable();
			self.ModifiedDt = ko.observable();
			self.Action = ko.observable();
			self.Caller = ko.observable();
			self.Role = ko.observable();
			self.PageSize = ko.observable();
			self.PageNumber = ko.observable();
			self.SortColumn = ko.observable();
			self.SortType = ko.observable();
			self.TotalRows = ko.observable();
    
    self.LogCategoryList = ko.observableArray([]);
    self.setLogCategory = function (
			LogCategoryID, LogCategoryName, TS, CreatedBy, CreatedDt, ModifiedBy, ModifiedDt, _Action, _Caller, _Role, _PageNumber, _PageSize, _SortColumn, _SortType, _TotalRows 
	
	) {

	LogCategoryID = _LogCategoryID;
			LogCategoryName = _LogCategoryName;
			TS = _TS;
			CreatedBy = _CreatedBy;
			CreatedDt = _CreatedDt;
			ModifiedBy = _ModifiedBy;
			ModifiedDt = _ModifiedDt;
			Action = _Action;
			Caller = _Caller;
			Role = _Role;
			PageSize = _PageSize
			PageNumber = _PageNumber;
			SortColumn = _SortColumn;
			SortType = _SortType;
			TotalRows = _TotalRows;
        
    }; //  End of set procedure 



    //Add Application
    self.AddLogCategory = function () {
        var self = this;
        self.Action = "Add";
        self.Caller = "trooks";
        self.Role = "user";

        $(".form").validate({ rules: { txtApplicationName: { required: true } } });
        if (!$(".form").valid()) {
            //alert("Invalied");
            return;
        }
        jQuery.ajax({
            url: "./api/LogCategoryapi/",
            data: ko.toJSON({ ApplicationName: self.ApplicationName, Action: self.Action, Caller: self.Caller }),
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                //alert(result);
                $('#myModaladd').modal('hide');
                self.GetApplicationByParameter();
            },
            error: function (xhr, ajaxOptions, thrownError) {
                $('#error-dis').text(thrownError);
                $('#register-error').css({ 'display': 'block' });
                $('#SearchFilter-home').css({ 'display': 'none' });
                $('#myModaladd').modal('hide');
                $(document).scrollTop(0);
            }
        });
        $(".form")[0].reset();
    };

    self.LoadApplication = function (appVal) {
        self.LoadingApplication(appVal);
    };

    //Delete Application
    self.DeleteApplicationAction = function (appval) {
        self.Action = "Remove";
        self.Caller = "trooks";

        jQuery.ajax({
            url: "./api/ApplicationApi/",
            data: ko.toJSON({ Action: self.Action, Caller: self.Caller, ApplicationID: appval.ApplicationID, TS: appval.TS }),
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                $('#myModal').modal('hide');
                self.GetApplicationByParameter();
            },
            error: function (xhr, ajaxOptions, thrownError) {
                $('#error-dis').text(thrownError);
                $('#register-error').css({ 'display': 'block' });
                $('#SearchFilter-home').css({ 'display': 'none' });
                $('#myModal').modal('hide');
                $(document).scrollTop(0);
            }
        });
    };
    //Edit Application

    self.EditApplication = function (appVal) {
        self.EditingApplication(appVal);
    };

    self.UpdateApplication = function (appval) {
        self.Action = "Update";
        self.Caller = "trooks";
        self.Role = "user";

        $(".form1").validate({ rules: { txtApplicationName: { required: true } } });
        if (!$(".form1").valid()) {
            return;
        }
        jQuery.ajax({
            url: "./api/ApplicationAPI/",
            data: ko.toJSON({ ApplicationName: appval.ApplicationName, Action: self.Action, Caller: self.Caller, ApplicationID: appval.ApplicationID, TS: appval.TS, CreatedBy: appval.CreatedBy, CreatedDt: appval.CreatedDt, ModifiedBy: appval.ModifiedBy, ModifiedDt: appval.ModifiedDt }),
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                $('#myModalEdit').modal('hide');
                self.GetApplicationByParameter();
            },
            error: function (xhr, ajaxOptions, thrownError) {
                $('#error-dis').text(thrownError);
                $('#register-error').css({ 'display': 'block' });
                $('#SearchFilter-home').css({ 'display': 'none' });
                $('#myModalEdit').modal('hide');
                $(document).scrollTop(0);
            }

        });



    };


    ko.bindingHandlers.sort = {
        init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var asc = false;
            element.style.cursor = 'pointer';

            element.onclick = function () {
                var value = valueAccessor();
                var prop = value.prop;
                var data = value.arr;

                asc = !asc;
                if (asc) {
                    self.ApplicationList.sort(function (left, right) {
                        return left[prop] == right[prop] ? 0 : left[prop] < right[prop] ? -1 : 1;
                    });
                } else {
                    self.ApplicationList.sort(function (left, right) {
                        return left[prop] == right[prop] ? 0 : left[prop] > right[prop] ? -1 : 1;
                    });
                }
            }
        }
    };

    //loading model popup
    ko.bindingHandlers['modal'] = {
        init: function (element, valueAccessor, allBindingsAccessor) {
            var allBindings = allBindingsAccessor();
            var $element = $(element);
            $element.addClass('hide modal');

            if (allBindings.modalOptions) {
                if (allBindings.modalOptions.beforeClose) {
                    $element.on('hide', function () {
                        return allBindings.modalOptions.beforeClose();
                    });
                }
            }

            return ko.bindingHandlers['with'].init.apply(this, arguments);
        },
        update: function (element, valueAccessor) {
            var value = ko.utils.unwrapObservable(valueAccessor());

            var returnValue = ko.bindingHandlers['with'].update.apply(this, arguments);

            if (value) {
                $(element).modal('show');
            } else {
                $(element).modal('hide');
            }

            return returnValue;
        }
    };

    //filter

    self.LogCategoryList = ko.observableArray([]);
    self.filter = ko.observable("");

    self.filteredLogcat = ko.computed(function () {
        var filterText = self.filter().toLowerCase();
        return ko.utils.arrayFilter(self.LogCategoryList(), function (item) {
            return filterText.length < 1 || item.LogCategoryID == filterText || item.CreatedBy == filterText || item.ModifiedBy == filterText || item.LogCategoryName.toLowerCase().indexOf(filterText) != -1
        });

    }, self);



    self.GetLogCatByParameter = function () {
        self.Action = "GetAll";
        self.Caller = "trooks";
        self.Role = "user";
        self.PageNumber = 1;
        self.PageSize = 10;
        self.SortColumn = "LogCategoryID";
        self.SortType = "asc";
        jQuery.ajax({
            url: "./api/LogCategoryapi/",
            data: { Action: self.Action, Caller: self.Caller, Role: self.Role, PageNumber: self.PageNumber, PageSize: self.PageSize, SortColumn: self.SortColumn, SortType: self.SortType },
            type: 'GET',
            success: function (result) {
                self.LogCategoryList.removeAll();
                $.each(result, function (key, val) {
                    self.LogCategoryList.push(
                        val
                    )
                })
            },
            error: function (xhr, ajaxOptions, thrownError) {
                $('#error-dis').text(thrownError);
                $('#register-error').css({ 'display': 'block' });
                $('#SearchFilter-home').css({ 'display': 'none' });
                $(document).scrollTop(0);
            }
        });

        if (loginUserRole.indexOf('super admin') <= -1) {
            $('.add-btn').attr('disabled', 'disabled');
        }
    };



	
 

}; //End of view model





